import 'package:app_info/app_info.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:url_launcher/url_launcher_string.dart';

class Utils {
  // 全局收起键盘
  static hideKeyBoard() {
    try {
      FocusManager.instance.primaryFocus?.unfocus();
    } catch (e){
      rethrow;
    }
  }

  static void showToast(String msg) {
    Fluttertoast.showToast(
      msg: msg,
      backgroundColor: Colors.black45,
      fontSize: 14
    );
  }

  static appLaunch(String url) async {
    if (await canLaunchUrlString(url)) {
      launchUrlString(url, mode: LaunchMode.externalApplication);
    }
  }

  static toGooglePlay(String idOrUrl) async {
    if(idOrUrl.startsWith('http')){
      Utils.appLaunch(idOrUrl);
    }else{
      if (idOrUrl.isEmpty) {
        idOrUrl = AppInfo().packageName;
      }
      final Uri toGoogleUri = Uri.https('play.google.com', '/store/apps/details', {'id': idOrUrl});
      Utils.appLaunch(toGoogleUri.toString());
    }
  }

  // 添加网络检测
  static Future<bool> checkNetWork() async {
    ConnectivityResult result = await Connectivity().checkConnectivity();
    if (result == ConnectivityResult.none) {
      return false;
    } else {
      return true;
    }
  }

}